package com.diandian.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @description StringUtil
 * @author TDW
 * @date 2018年8月8日
 *
 */
public class StringUtil extends StringUtils {
	private static final Logger LOGGER = LoggerFactory.getLogger(StringUtils.class);

	public static String parseUnicode(String line) {
		int len = line.length();
		char[] out = new char[len];// 保存解析以后的结果
		int outLen = 0;
		try {
			for (int i = 0; i < len; i++) {
				char aChar = line.charAt(i);
				if (aChar == '\\') {
					aChar = line.charAt(++i);
					if (aChar == 'u') {
						int value = 0;
						for (int j = 0; j < 4; j++) {
							aChar = line.charAt(++i);
							switch (aChar) {
							case '0':
							case '1':
							case '2':
							case '3':
							case '4':
							case '5':
							case '6':
							case '7':
							case '8':
							case '9':
								value = (value << 4) + aChar - '0';
								break;
							case 'a':
							case 'b':
							case 'c':
							case 'd':
							case 'e':
							case 'f':
								value = (value << 4) + 10 + aChar - 'a';
								break;
							case 'A':
							case 'B':
							case 'C':
							case 'D':
							case 'E':
							case 'F':
								value = (value << 4) + 10 + aChar - 'A';
								break;
							default:
								throw new IllegalArgumentException("Malformed \\uxxxx encoding.");
							}
						}
						out[outLen++] = (char) value;
					} else {
						if (aChar == 't')
							aChar = '\t';
						else if (aChar == 'r')
							aChar = '\r';
						else if (aChar == 'n')
							aChar = '\n';
						else if (aChar == 'f')
							aChar = '\f';
						out[outLen++] = aChar;
					}
				} else {
					out[outLen++] = aChar;
				}
			}
		} catch (Exception e) {
			LOGGER.info("uncode转码失败", e);
		}
		return new String(out, 0, outLen);
	}

	/**
	 * @description getRightfulStr 只保留大小写字母、中文、顿号（、）逗号（,）（，）英文句号（.）
	 * @param str
	 * @return
	 * @author TDW
	 * @date 2018年8月7日
	 * @modify_date
	 * @modify_author
	 * @modify_desc
	 */
	public static String getRightfulStr(String str) {
		if (StringUtils.isNotBlank(str)) {
			return str.replaceAll("[^a-zA-Z0-9\\u4e00-\\u9fa5\\、\\,\\，\\.]", "");
		}
		return "";
	}

	/**
	 * @description getExt
	 * @param oldpath
	 * @return
	 * @author TDW
	 * @date 2018年8月8日
	 * @modify_date
	 * @modify_author
	 * @modify_desc
	 */
	public static String getExt(String oldpath) {
		return oldpath.substring(oldpath.lastIndexOf(".") + 1, oldpath.length());
	}

	
	/**
	 * @description getStrLastNum
	 * @param str
	 * @return
	 * @author TDW
	 * @date 2018年8月11日
	 * @modify_date 
	 * @modify_author
	 * @modify_desc
	 */
	public static String[] getStrLastNum(String str){
		Pattern pattern = Pattern.compile("\\d+$");
		Matcher matcher = pattern.matcher(str);
		if (matcher.find()) {
			String num = matcher.group();
			str = str.substring(0,str.lastIndexOf(num));
			return new String[]{str,num};
			
		}
		return new String[]{str};
	}
	
	
	public static void main(String[] args) {
		String sa = new String("abc12.3");
		
		String[] strs = getStrLastNum(sa);
		for(String str : strs)
			System.out.println(str);
	}

}
